//
//  YWCirclePhotoViewController.m
//  聆听FM
//
//  Created by dllo on 16/6/8.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "YWCirclePhotoViewController.h"
#import "YWPicDetailModel.h"
#import "YWCircleDetailTableViewCell.h"
#import "AlbumDetailViewController.h"
#import "ZCSListenListListModel.h"
#import "ZCSPlayMusicViewController.h"
#import "CBStoreHouseRefreshControl.h"
@interface YWCirclePhotoViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *circleDetailTableView;

@property (nonatomic, retain) NSMutableArray *circleDetailArray;

@property (nonatomic, assign) NSInteger pageId;


@property (nonatomic, assign) NSInteger maxPageId;

@property (nonatomic, retain) NSMutableArray *playArray;
@property (nonatomic, strong) CBStoreHouseRefreshControl *storeHouseRefreshControl;
@end

@implementation YWCirclePhotoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.titleString;
    [self getView];
    self.circleDetailArray = [NSMutableArray array];
    self.playArray = [NSMutableArray array];
    _pageId = 1;
    [self getDataByPageId:_pageId];
}
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}


- (void)getView
{
    self.circleDetailTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT) style:UITableViewStylePlain];
    _circleDetailTableView.delegate = self;
    _circleDetailTableView.dataSource = self;
    _circleDetailTableView.separatorStyle = 0;
    _circleDetailTableView.sectionHeaderHeight = 0;
    _circleDetailTableView.sectionFooterHeight = 0;
    [self.view addSubview:_circleDetailTableView];
    
    //注册cell
    [_circleDetailTableView registerClass:[YWCircleDetailTableViewCell class] forCellReuseIdentifier:@"circleCell"];
    [_circleDetailTableView registerClass:[YWCircleDetailTableViewCell class] forCellReuseIdentifier:@"colorNumCell"];
    
    
    
    self.storeHouseRefreshControl = [CBStoreHouseRefreshControl attachToScrollView:self.circleDetailTableView target:self refreshAction:@selector(refreshTriggered:) plist:@"LTFM" color:[UIColor blackColor] lineWidth:3 dropHeight:80 scale:1.2 horizontalRandomness:300 reverseLoadingAnimation:NO internalAnimationFactor:0.7];
    
    _circleDetailTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _pageId++;
        
        if (_pageId > _maxPageId) {
            [_circleDetailTableView.mj_footer endRefreshingWithNoMoreData];
        }
        else
        {
            [self getDataByPageId:_pageId];
        }
        
        [self getDataByPageId:_pageId];
        
        
    }];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.storeHouseRefreshControl scrollViewDidScrollWithType:1];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self.storeHouseRefreshControl scrollViewDidEndDragging];
}

- (void)refreshTriggered:(id)sender
{
    
    [self performSelector:@selector(finishRefreshControl) withObject:nil afterDelay:2 inModes:@[NSRunLoopCommonModes]];
}

- (void)finishRefreshControl
{
    [_circleDetailArray removeAllObjects];
    [_circleDetailTableView reloadData];
    
    _pageId = 1;
    [self getDataByPageId:_pageId];
    
    [self getDataByPageId:1];
}


#pragma mark ------ 代理方法 ------

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _circleDetailArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    YWPicDetailModel *detailModel = [_circleDetailArray objectAtIndex:indexPath.row];
    if (indexPath.row < 4) {
        YWCircleDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"colorNumCell"];
        cell.detailModel = detailModel;
        cell.numLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
        if (indexPath.row == 0) {
            cell.numLabel.textColor = [UIColor redColor];
        }
        else if (indexPath.row == 1)
        {
            cell.numLabel.textColor = [UIColor orangeColor];
        }
        else if (indexPath.row == 2)
        {
            cell.numLabel.textColor = [UIColor greenColor];
        }
        return cell;
    }
    YWCircleDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"circleCell"];
    cell.detailModel = detailModel;
    cell.numLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YWPicDetailModel *detailModel = [_circleDetailArray objectAtIndex:indexPath.row];
    NSString *string = detailModel.title;
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14], NSFontAttributeName, nil];
    CGRect newRect = [string boundingRectWithSize:CGSizeMake(290 * FITWIDTH, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return newRect.size.height + 45 * FITHEIGHT;
}

- (void)getDataByPageId:(NSInteger)pageId
{
    
    NSString *urlString = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v2/rankingList/track?device=iPhone&key=%@&pageId=%ld&pageSize=20&scale=3", _key, pageId];
    
    [LTNetTool GetNetWithURL:urlString Body:nil Header:nil Response:LTJSON Success:^(id result) {
        _maxPageId = [[result objectForKey:@"maxPageId"] integerValue];
        NSArray *tempArray = [result objectForKey:@"list"];
        for (NSDictionary *dic in tempArray) {
            YWPicDetailModel *detailModel = [[YWPicDetailModel alloc] initWithDic:dic];
            ZCSListenListListModel *playModel = [[ZCSListenListListModel alloc] initWithDic:dic];
            [_playArray addObject:playModel];
            [_circleDetailArray addObject:detailModel];
        }
        [_circleDetailTableView reloadData];
        [_storeHouseRefreshControl finishingLoading];
        [_circleDetailTableView.mj_footer endRefreshing];
    } Failure:^(NSError *error) {
        
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ZCSPlayMusicViewController *play = [ZCSPlayMusicViewController sharePlayVC];
    play.key = self.key;
    play.musicIndex = indexPath.row;
    play.dataArray = self.playArray;
    play.maxPage = self.maxPageId;
    
    [self presentViewController:play animated:YES completion:nil];
    
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
